package com.qf.dao.impl;

import com.qf.dao.OrderDao;
import com.qf.domain.Order;
import com.qf.domain.OrderDetail;
import com.qf.utils.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    private QueryRunner queryRunner = new QueryRunner();

    @Override
    public void add(Order order) {
        Connection connection = DataSourceUtil.getConnection();
        String sql = "  INSERT INTO db_shopping.`tb_order`(`id`,`uid`,`money`,`status`,`time`,`aid`) VALUES (?,?,?,?,?,?)  ";
        Object[] params = {order.getId(), order.getUid(), order.getMoney(), order.getStatus(), order.getTime(), order.getAid()};
        try {
            queryRunner.update(connection, sql, params);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        } finally {
            DataSourceUtil.closeAll(null, null, connection);
        }
    }

    @Override
    public void addDetail(OrderDetail orderDetail) {
        Connection connection = DataSourceUtil.getConnection();
        String sql = " iNSERT INTO db_shopping.`tb_orderdetail` (`oid`,`pid`,`num`,`money`) VALUES (?,?,?,?) ";
        Object[] params = {orderDetail.getOid(), orderDetail.getPid(), orderDetail.getNum(), orderDetail.getMoney()};
        try {
            queryRunner.update(connection, sql, params);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        } finally {
            DataSourceUtil.closeAll(null, null, connection);
        }
    }

    @Override
    public List<Order> select(Integer uid) {
        Connection connection = DataSourceUtil.getConnection();
        String sql = " select * from db_shopping.tb_order where uid=? ";
        try {
            return queryRunner.query(connection, sql, new BeanListHandler<>(Order.class), uid);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        } finally {
            DataSourceUtil.closeAll(null, null, connection);
        }
    }

    @Override
    public Order selectByoid(String oid) {
        Connection connection = DataSourceUtil.getConnection();
        String sql = " select * from db_shopping.tb_order where id=? ";
        try {
            return queryRunner.query(connection, sql, new BeanHandler<>(Order.class), oid);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        } finally {
            DataSourceUtil.closeAll(null, null, connection);
        }
    }

    @Override
    public List<OrderDetail> selectDetail(String oid) {
        Connection connection = DataSourceUtil.getConnection();
        String sql = " select * from db_shopping.tb_orderdetail where oid=? ";
        try {
            return queryRunner.query(connection, sql, new BeanListHandler<>(OrderDetail.class), oid);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        } finally {
            DataSourceUtil.closeAll(null, null, connection);
        }
    }

    @Override
    public List<Order> selectLIST() {
        Connection connection = DataSourceUtil.getConnection();
        String sql = " select * from db_shopping.tb_order ";
        try {
            return queryRunner.query(connection, sql, new BeanListHandler<>(Order.class));
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        } finally {
            DataSourceUtil.closeAll(null, null, connection);
        }
    }

    @Override
    public List<Order> selectstatus(String status) {
        Connection connection = DataSourceUtil.getConnection();
        try {
            if (status.equals("0")) {
                String sql = " select * from db_shopping.tb_order  ";
                return queryRunner.query(connection, sql, new BeanListHandler<>(Order.class));
            } else {
                String sql = " select * from db_shopping.tb_order where status=? ";
                return queryRunner.query(connection, sql, new BeanListHandler<>(Order.class), status);
            }
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        } finally {
            DataSourceUtil.closeAll(null, null, connection);
        }
    }
}
